package com.zhentao;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.zhentao.core.ChargeCoreFeignApi;
import com.zhentao.user.ChargeUserFeignApi;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.Date;

@SpringBootApplication // springboot 启动类
@MapperScan("com.zhentao.mapper")
@EnableDiscoveryClient
@RefreshScope
@EnableFeignClients(clients = {ChargeUserFeignApi.class, ChargeCoreFeignApi.class})
@EnableAsync // 开启异步调用
public class ChargeMarketApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChargeMarketApplication.class, args);
    }

    // mybatis-plus 乐观锁的支持
    @Bean
    public MybatisPlusInterceptor interceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean
    public MetaObjectHandler metaObjectHandler(){
        MetaObjectHandler metaObjectHandler = new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
            }
        };
        return metaObjectHandler;
    }
}
